package com.dz.risk.controller;

import com.dz.risk.common.ResponseVO;
import com.dz.risk.pojo.domain.ReportFormsDataValueEntity;
import com.dz.risk.pojo.model.FormulaExtModel;
import com.dz.risk.service.DynamicReportFormsService;
import com.dz.risk.service.ReportFormsDataValueService;
import com.dz.risk.validation.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;

/**
 * 报表数据值Api
 *
 * @author Doke
 * @date 2021/7/29 10:42
 */
@Api(tags = "报表数据值Api")
@RestController
@RequestMapping("/dz-ftsp/stest/reportFormsDataValue")
public class ReportFormsDataValueController {

    private final ReportFormsDataValueService reportFormsDataValueService;

    @Autowired
    public ReportFormsDataValueController(ReportFormsDataValueService reportFormsDataValueService) {
        this.reportFormsDataValueService = reportFormsDataValueService;
    }

    @ApiOperation("根据ID更新报表数据值")
    @PostMapping("/updateById")
    public ResponseVO updateById(@RequestBody @Validated(Update.class) ReportFormsDataValueEntity reportFormsDataValueEntity) {
        reportFormsDataValueService.updateById(reportFormsDataValueEntity);
        return ResponseVO.ok();
    }

    /**
     * 设置报表数据项公式
     */
    @ApiOperation("设置报表数据项公式")
    @PostMapping("/cellFormulaSetUp")
    public ResponseVO cellFormulaSetUp(@Validated(Update.class) @RequestBody FormulaExtModel formulaExtModel) {
        reportFormsDataValueService.updateFormulaById(formulaExtModel);
        return ResponseVO.ok();
    }

    /**
     * 查询报表计算版本数据
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportFormsId", value = "报表Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "version", value = "计算版本 : yyyy-MM-dd HH:mm:ss", required = true, dataType = "date")
    })
    @ApiOperation("查询报表计算版本数据")
    @GetMapping("/selectReportFormsDataValueByVersion")
    public ResponseVO selectReportFormsDataValueByVersion(@RequestParam("reportFormsId") String reportFormsId, @RequestParam("version") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date version) {
        return ResponseVO.ok(reportFormsDataValueService.selectReportFormsDataValueByVersion(reportFormsId, version));
    }

    /**
     * 导入临时表数据
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reportFormsId", value = "报表Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "version", value = "计算版本 : yyyy-MM-dd HH:mm:ss", required = true, dataType = "date")
    })
    @ApiOperation("导出报表计算版本数据")
    @GetMapping("/doExportReportFormsDataValueByVersion")
    public void doExportReportFormsDataValueByVersion(HttpServletResponse response,@RequestParam("reportFormsId") String reportFormsId, @RequestParam("version") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date version) throws IOException {
        reportFormsDataValueService.doExportReportFormsDataValueByVersion(response,reportFormsId,version);

    }

}
